package org.security.filter;

import org.security.config.*;
import org.security.config.web.*;
import org.security.handler.FilterHandler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jt
 * @date 2024/7/20 16:42
 */

@Component
public class FilterHandlerManager implements ApplicationContextAware  {

    private ApplicationContext context;

    @Autowired
    private FreeProperties freeProperties;
    
    @Autowired
    private AbstractFreeSecurityWebConfig config;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context=applicationContext;
    }

    void exec(HttpServletRequest request, HttpServletResponse response){

        Map<String, FilterHandler> beansOfType = context.getBeansOfType(FilterHandler.class);
        List<FilterHandler> filterList = beansOfType.values().stream().sorted(Comparator.comparing(res -> res.getIndex()))
                .collect(Collectors.toList());

        //是否走认证授权
        boolean ifcatZat = config.urlPermissionConfig.getUrlPermissionList().stream()
                .filter(res -> res.getPermission().equals(UrlPermissionConfig.BasePermissionType.all.name()) && request.getRequestURI().equals(res.getUrl()))
                .count() > 0 ? true : false;

        boolean ifzat = config.urlPermissionConfig.getUrlPermissionList().stream()
                .filter(res -> res.getPermission().equals(UrlPermissionConfig.BasePermissionType.noAuthortization.name()) && request.getRequestURI().equals(res.getUrl()))
                .count() > 0 ? true : false;

        //判断是否认证
        if(!freeProperties.getAuthEnable()){
            filterList = filterList.stream().filter(res->!res.getName().equals("UsernamePasswordAuthoricationHandler"))
                   .collect(Collectors.toList());
        }
        //是否开启授权
        if(!freeProperties.getAuthorizeEnable()){
            filterList = filterList.stream().filter(res->!res.getName().equals("AuthorizationHandler"))
                    .collect(Collectors.toList());
        }
        if(ifzat){
            filterList = filterList.stream().filter(res->!res.getName().equals("AuthorizationHandler"))
                    .collect(Collectors.toList());
        }
        //url是否需要认证授权
        if(ifcatZat){
            filterList = filterList.stream().filter(res->!res.getName().equals("AuthorizationHandler") && !res.getName().equals("UsernamePasswordAuthoricationHandler"))
                    .collect(Collectors.toList());
        }
        // TODO: 2024/7/21 检查config中的url权限
        filterList.forEach(res->{res.doFilter(request,response); });


    }



}
